﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ComicIsland.Common;
using ComicIsland.Common.Entities.LocalJson;

/**
 * 创建时间：2014/12/23 17:37:41
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.CIHelper
{
    /// <summary>
    /// App的书架管理
    /// </summary>
    public class LocalUserBookStoreAlbumHelper
    {
        private LocalCacheDataHelper cacheHelper = new LocalCacheDataHelper();

        public LocalUserBookStoreAlbumHelper()
        {
            //不对缓存进行刷新操作
            cacheHelper.GetDataFromLocalCache(KEYS.KEY_LocalUserBookStoreAlbum, false);
        }

        /*
        /// <summary>
        /// 根据ID判断缓存中是否存在漫画
        /// </summary>
        /// <param name="comicId"></param>
        /// <returns></returns>
        public bool IsExistComicFromLocalUserBookStoreAlbum(int comicId)
        {
            bool result = false;
            try
            {
                string localCacheData = cacheHelper.LocalCacheData;
                //
                ObservableCollection<LocalUserBookStoreAlbum> localBookStores = new ObservableCollection<LocalUserBookStoreAlbum>();
                if (string.IsNullOrWhiteSpace(localCacheData))
                {
                    return false;
                }

                localBookStores = JSONHelper.Deserialize<ObservableCollection<LocalUserBookStoreAlbum>>(localCacheData);
                if (localBookStores != null && localBookStores.Count > 0)
                {
                    foreach (LocalUserBookStoreAlbum item in localBookStores)
                    {
                        if (item == null)
                            continue;
                        if (item.ComicId == comicId)
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
                MessageToastManager.Show("收藏操作失败！");
            }
            return result;
        }

        /// <summary>
        /// 添加漫画至书架
        /// </summary>
        /// <param name="localBookStore">LocalUserBookStoreAlbum</param>
        public bool AddOrUpdateComicToLocalUserBookStoreAlbum(LocalUserBookStoreAlbum localBookStore)
        {
            bool result = false;
            if (!(localBookStore != null && localBookStore.ComicId > 0))
            {
                //MessageToastManager.Show("数据不完整，无法进行收藏操作！");
                return false;
            }

            try
            {
                string localCacheData = cacheHelper.LocalCacheData;
                //
                ObservableCollection<LocalUserBookStoreAlbum> localBookStores = new ObservableCollection<LocalUserBookStoreAlbum>();
                if (string.IsNullOrWhiteSpace(localCacheData))
                {
                    //书架为空
                    localBookStores.Add(localBookStore);
                }
                else
                {
                    localBookStores = JSONHelper.Deserialize<ObservableCollection<LocalUserBookStoreAlbum>>(localCacheData);
                    if (!(localBookStores != null && localBookStores.Count > 0))
                    {
                        localBookStores = new ObservableCollection<LocalUserBookStoreAlbum>();
                    }
                    //
                    if (!IsExistComicFromLocalUserBookStoreAlbum(localBookStore.ComicId))
                    {
                        localBookStores.Add(localBookStore);
                    }
                    else
                    {
                        var item = (from p in localBookStores where p.ComicId == localBookStore.ComicId select p).FirstOrDefault<LocalUserBookStoreAlbum>();
                        if (item != null)
                        {
                            item.BookId = localBookStore.BookId;
                            item.SourceName = localBookStore.SourceName;
                        }
                    }
                }
                //
                string json = JSONHelper.Serialize<ObservableCollection<LocalUserBookStoreAlbum>>(localBookStores);
                cacheHelper.SetDataToLocalCache(KEYS.KEY_LocalUserBookStoreAlbum, json);
                //
                //MessageToastManager.Show("收藏操作成功！");
                result = true;
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
                //MessageToastManager.Show("收藏操作失败！");
            }
            return result;
        }

        /// <summary>
        /// 获取已收藏或缓存的所有书籍
        /// </summary>
        /// <returns></returns>
        public ObservableCollection<LocalUserBookStoreAlbum> GetComicFromLocalUserBookStoreAlbum()
        {
            ObservableCollection<LocalUserBookStoreAlbum> localBookStores = null;
            try
            {
                string localCacheData = cacheHelper.LocalCacheData;
                //

                if (string.IsNullOrWhiteSpace(localCacheData))
                {
                    return localBookStores;
                }

                localBookStores = JSONHelper.Deserialize<ObservableCollection<LocalUserBookStoreAlbum>>(localCacheData);
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            }

            return localBookStores;
        }

        public LocalUserBookStoreAlbum GetOneComicFromLocalUserBookStoreAlbum(int comicId)
        {
            var items = this.GetComicFromLocalUserBookStoreAlbum();
            if (items != null && items.Count > 0)
            {
                return items.Where<LocalUserBookStoreAlbum>(p => p.ComicId == comicId).FirstOrDefault<LocalUserBookStoreAlbum>();
            }
            return null;
        }

        public bool RemoveComicFromLocalUserBookStoreAlbum(int comicId)
        {
            bool result = false;
            try
            {
                var items = this.GetComicFromLocalUserBookStoreAlbum();
                if (!(items != null && items.Count > 0))
                {
                    return result;
                }

                for (int i = 0; i < items.Count; i++)
                {
                    var item = items[i];
                    if (item == null)
                        continue;
                    if (item.ComicId == comicId)
                    {
                        this.RemoveComicFromLocalUserBookStoreAlbum(item);
                    }
                }
                result = true;
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            }
            return result;
        }

        /// <summary>
        /// 从书架上移除书籍
        /// <para>1.书架缓存移除</para>
        /// <para>2.下载过的图片也移除</para>
        /// </summary>
        /// <param name="localCacheData"></param>
        public bool RemoveComicFromLocalUserBookStoreAlbum(LocalUserBookStoreAlbum localBookStore)
        {
            bool result = false;
            //TODO 2.下载过的图片也移除
            try
            {
                string localCacheData = cacheHelper.LocalCacheData;
                //
                ObservableCollection<LocalUserBookStoreAlbum> localBookStores = new ObservableCollection<LocalUserBookStoreAlbum>();
                if (string.IsNullOrWhiteSpace(localCacheData))
                {
                    return result;
                }
                else
                {
                    localBookStores = JSONHelper.Deserialize<ObservableCollection<LocalUserBookStoreAlbum>>(localCacheData);
                    if (localBookStores != null && localBookStores.Count > 0)
                    {
                        //localBookStores.Remove(localBookStore);
                        for (int i = 0; i < localBookStores.Count; i++)
                        {
                            var item = localBookStores[i];
                            if (item == null)
                                continue;
                            if (item.ComicId == localBookStore.ComicId)
                            {
                                localBookStores.RemoveAt(i);
                                break;
                            }
                        }
                        string json = JSONHelper.Serialize<ObservableCollection<LocalUserBookStoreAlbum>>(localBookStores);
                        cacheHelper.SetDataToLocalCache(KEYS.KEY_LocalUserBookStoreAlbum, json);
                        result = true;
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogInfo(err.ToString(), this.GetType(), MethodBase.GetCurrentMethod());
            }

            return result;
        }

        /// <summary>
        /// 更新漫画的当前阅读章节
        /// </summary>
        /// <param name="comicId"></param>
        /// <param name="chapterId"></param>
        /// <param name="chapterTitle"></param>
        /// <returns></returns>
        public bool UpdateComicFromLocalUserBookStoreAlbum(int comicId, int chapterId, string chapterTitle)
        {
            bool result = false;
            var items = this.GetComicFromLocalUserBookStoreAlbum();
            if (!(items != null && items.Count > 0))
            {
                return result;
            }

            foreach (var item in items)
            {
                if (item == null)
                    continue;
                if (item.ComicId == comicId)
                {
                    item.ChapterId = chapterId;
                    item.ChapterName = chapterTitle;
                    string json = JSONHelper.Serialize<ObservableCollection<LocalUserBookStoreAlbum>>(items);
                    cacheHelper.SetDataToLocalCache(KEYS.KEY_LocalUserBookStoreAlbum, json);
                    result = true;
                    break;
                }
            }
            return result;
        }
         */
    }
}
